import numpy as np
import matplotlib.pyplot as plt

# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']

categories = ['第一季度', '第二季度', '第三季度', '第三季度']
data_sh = [15, 22, 18, 25]
data_sz = [20, 16, 24, 19]
bar_width = 0.35
x = np.arange(len(categories))

bar_sh = plt.bar(x - bar_width/2, data_sh, width=bar_width, label='上海')
bar_sz = plt.bar(x + bar_width/2, data_sz, width=bar_width, label='苏州')
# 添加数值标记
def autolabel(bars):
    for bar in bars:
        height = bar.get_height()
        plt.text(bar.get_x() + bar.get_width()/2., height,
                 f'{height}',
                 ha='center', va='bottom')

autolabel(bar_sh)
autolabel(bar_sz)

plt.xticks(x, categories)
plt.xlabel('季度')
plt.ylabel('总产值(万元)')
plt.title('Dual Bar Chart')
plt.legend()
plt.tight_layout()  # 自动调整布局
plt.show()